package sample.reflects;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class HelloInvokeSetAndGetCls {
	public static void main(String[]args){
		Class<?> demo=null;
		Object obj=null;
		try {
			demo=Class.forName("sample.reflects.Person3");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			obj=demo.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		setter(obj,"Sex","man",String.class);
		getter(obj,"Sex");
	}

	private static void getter(Object obj, String string) {
		try {
			Method method=obj.getClass().getMethod("get"+string);
			try {
				System.out.println(method.invoke(obj));
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}

	private static void setter(Object obj, String att, String value,
			Class<?>  type) {
		Method method = null;
		try {
			method = obj.getClass().getMethod("set"+att, type);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
		try {
			method.invoke(obj, value);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}
